# Import library of pandas and numpy
import numpy as np
import pandas as pd
You can convert a list,numpy array, or dictionary to a Series:
# Defining values to use in later stage
labels = ['a','b','c']
my_list = [10,20,30]
arr = np.array([10,20,30])
d = {'a':10,'b':20,'c':30}
Using above created Lists
pd.Series(data=my_list)
pd.Series(data=my_list,index=labels)
pd.Series(my_list,labels)
NumPy Arrays
pd.Series(arr)
pd.Series(arr,labels)
Dictionary
pd.Series(d)
A pandas Series can hold a variety of object types:
pd.Series(data=labels)
# Even functions (although unlikely that you will use this)
pd.Series([sum,print,len])
The key to using a Series is understanding its index. Pandas makes use of these index names or numbers by allowing for fast look ups of information (works like a hash table or dictionary).
Let's see some examples of how to grab information from a Series. Let us create two sereis, ser1 and ser2:
ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan'])
ser1
ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan'])
ser2
ser1['USA']
Operations are then also done based off of index:
ser1 + ser2
Let's stop here for now and move on to next chapter for DataFrames, which will expand on the concept of Series!